﻿using System;
using System.Web.UI;
using System.Web.UI.WebControls;
using TheOutfield.UmbExt.UniversalMediaPicker.Extensions;
using TheOutfield.UmbExt.UniversalMediaPicker.Controls;

namespace TheOutfield.UmbExt.UniversalMediaPicker.Providers.Picasa
{
    public class PicasaCreateControl : AbstractCreateControl
    {
        private PicasaProvider provider;
        private PicasaConfig config;
        private FileUpload fileFileUpload = new FileUpload();
        private TextBox titleTextBox = new TextBox();
        private TextBox descriptionTextBox = new TextBox();
        private TextBox tagsTextBox = new TextBox();
        private DropDownList collectionsDropDownList = new DropDownList();
        private DropDownList categoriesDropDownList = new DropDownList();

        public PicasaCreateControl(PicasaProvider provider, string options)
        {
            this.provider = provider;
            this.config = options.DeserializeJsonTo<PicasaConfig>();
        }

        protected override void CreateChildControls()
        {
            fileFileUpload.ID = "fileFileUpload";
            titleTextBox.ID = "titleTextBox";
            descriptionTextBox.ID = "descriptionTextBox";
            tagsTextBox.ID = "tagsTextBox";
            collectionsDropDownList.ID = "collectionsDropDownList";
            categoriesDropDownList.ID = "categoriesDropDownList";

            fileFileUpload.CssClass = "guiInputText guiInputStandardSize";
            titleTextBox.CssClass = "guiInputText guiInputStandardSize";
            descriptionTextBox.CssClass = "guiInputText guiInputStandardSize";
            tagsTextBox.CssClass = "guiInputText guiInputStandardSize";
            collectionsDropDownList.CssClass = "guiInputText guiInputStandardSize";
            categoriesDropDownList.CssClass = "guiInputText guiInputStandardSize";

            descriptionTextBox.TextMode = TextBoxMode.MultiLine;
            tagsTextBox.TextMode = TextBoxMode.MultiLine;

            Controls.Add(fileFileUpload);
            Controls.Add(titleTextBox);
            Controls.Add(descriptionTextBox);
            Controls.Add(tagsTextBox);
            Controls.Add(collectionsDropDownList);
            Controls.Add(categoriesDropDownList);
        }

        protected override void OnLoad(EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                //BindCollections();
                //BindCategories();
            }

            base.OnLoad(e);
        }

        //protected void BindCollections()
        //{
        //    collectionsDropDownList.Items.Clear();
        //    collectionsDropDownList.Items.Add(new ListItem("None", ""));
        //    collectionsDropDownList.Items.AddRange(provider.GetCollections());
        //}

        //protected void BindCategories()
        //{
        //    categoriesDropDownList.Items.Clear();
        //    categoriesDropDownList.Items.Add(new ListItem("None", ""));
        //    categoriesDropDownList.Items.AddRange(provider.GetCategories());
        //}

        protected override void Render(HtmlTextWriter writer)
        {
            writer.AddFormRow("File:", fileFileUpload);
            writer.AddFormRow("Title:", titleTextBox);
            writer.AddFormRow("Description:", descriptionTextBox);
            writer.AddFormRow("Tags:", "Separated with commas", tagsTextBox);
            writer.AddFormRow("Collection:", collectionsDropDownList);
            writer.AddFormRow("Category:", categoriesDropDownList);
        }

        public override bool TrySave(out MediaItem savedMediaItem, out string message)
        {
            savedMediaItem = null;
            message = "Not yet implemented";
            return false;
            //EnsureChildControls();

            //if (fileFileUpload.HasFile)
            //{
            //    try
            //    {
            //        savedMediaItem = provider.CreateMediaItem(fileFileUpload.FileContent,
            //            titleTextBox.Text,
            //            descriptionTextBox.Text,
            //            tagsTextBox.Text,
            //            Convert.ToInt32("0" + collectionsDropDownList.SelectedValue),
            //            Convert.ToInt32("0" + categoriesDropDownList.SelectedValue));

            //        message = "File created successfully";

            //        return true;
            //    }
            //    catch (Exception e)
            //    {
            //        savedMediaItem = null;
            //        message = "An error occured creating the media item: " + e.Message;

            //        return false;
            //    }
            //}
            //else
            //{
            //    savedMediaItem = null;
            //    message = "No file selected";

            //    return false;
            //}
        }
    }
}